/**
 * TaskQueueHandler.java
 * @author Daniel Liu
 * Create: 2007-1-1 ����12:50:51
 */
package org.lex.ftp.queue;

import java.io.File;
import java.io.IOException;

import javax.swing.JOptionPane;

import org.lex.ftp.FTP;
import org.lex.ftp.ui.explorer.FTPExplorer;
import org.lex.ftp.util.Sound;
import org.lex.progress.Progress;
import org.lex.utils.pool.DaemonExecutor;


public class QueueHandler implements Runnable {
	private static final boolean DEBUG = true;
	private boolean abort = false;
	private boolean transferring = false;
	private FTPExplorer explorer = null;
	private TaskHandler handler = null;
	private StatusMonitorTimer monitor = null;

	public QueueHandler(FTPExplorer explorer) {
		super();
		this.explorer = explorer;
		this.handler = new TaskHandler();
		this.monitor = new StatusMonitorTimer(explorer);
	}

	public void abort() {
		this.abort = true;
		QueueUtils.setTransferring(explorer.app, false);
		this.handler.abort();
	}

	public void transfer() {
		this.transferring = true;
		QueueUtils.setTransferring(explorer.app, true);
		DaemonExecutor.execute(this);
	}

	public boolean isTransferring() {
		return this.transferring;
	}

	public String getStatus() {
		this.monitor.refreshStatusPane();
		if (false == this.transferring)
			return "Idle.";
		else
			return this.handler.getStatusString();
	}

	public void run() {
		// stop keep-alive
		// explorer.getFtpClient().setKeepAlive( false );
		// backup current path
		FTP app = explorer.app;
		File fileSystemPathBackup = app.getFileExplorer().getDirectory();
		String ftpPathBackup = explorer.getFTPModel().getCatalog();
		ITransferQueue queue = app.getQueue();
		this.abort = false;
		while (false == abort && false == queue.isEmpty()) {
			Task task = queue.getFirst();
			ln(task);
			pause();
			if (task.hasChild()) {
				// a directory
				queue.removeFirst();
				task.expandChildren();
				continue;
			} else {
				// a file
				task.setStatus(Progress.Status.RUNNING);
				queue.fireQueueChanged(0);
				app.getStatusPane().setStatusString(task.getDescription());
				monitor.monitor(handler);
				try {
					handler.handle(task);
				} catch (IOException e) {
					e.printStackTrace();
				}
				queue.removeFirst();
			}
			queue.fireQueueChanged();
		}
		queue.fireQueueChanged();
		this.abort = false;
		// restore backuped path
		app.getFileExplorer().setDirectory(fileSystemPathBackup, true);
		this.explorer.getFTPModel().setCatalog(ftpPathBackup);
		// start keep-alive timer
		// this.explorer.getFtpClient().setKeepAlive( true );
		// queue transfer complete
		app.getGlobalLogger().misc(explorer.getFTPModel().getFTPURL(), "Transfer complete");
		app.getStatusPane().clear();
		app.getStatusPane().setStatusString("Queue transfer complete");
		Sound.complete();
		this.transferring = false;
		QueueUtils.setTransferring(explorer.app, false);
	}

	private StringBuilder buf = new StringBuilder();

	private void ln(Object... msgs) {
		if (DEBUG) {
			buf.setLength(0);
			for (Object o : msgs)
				buf.append(o);
			System.out.println(buf.toString());
		}
	}

	void pause() {
		if (false)
			JOptionPane.showMessageDialog(null, "Pause");
	}
}
